<?php
namespace App\Api\Appfc114;

use PhalApi\Api;
use PhalApi\Exception\BadRequestException;
use PhalApi\Request;
use App\Domain\Appfc114\MetaData as DomainMetaData;

/**
 * 配置数据接口
 *
 * @author: dogstar <chanzonghuang@gmail.com> 2014-10-04
 */

class DataBase extends Api {
    /**
     * 当前用户信息
     */
    public $user;

    public function __construct()
    {
        //允许所有域名发起的跨域请求
        header('Access-Control-Allow-Origin: *');  //支持全域名访问，不安全，部署后需要固定限制为客户端网址
    }

	public function getRules() {
        return array(
            "getBaseData" => [],
            "getMetaDataByType"=>[
                'code_type' 	=> array('name' => 'code_type', 'type'=>'string', 'require' => true, 'desc' => '类型id'),
            ]
        );
	}

    /**
     * 返回base数据
     * @return obj base数据
     */
    public function getBaseData() {
        $rs = \PhalApi\DI()->config->get('basecode');
        $data = [];
        foreach($rs as $k=>$v){
            $i=0;
            foreach ($v as $k1 => $v1){
                if(is_array($v1)){
                    $j = 0;
                    //$data[$k][$i]["key"] = $k1;
                    foreach ($v1 as $k2 => $v2){
                        $data[$k][$k1][$j]["key"] = $k2;
                        $data[$k][$k1][$j]["value"] = $v2;
                        $j++;
                    }
                }else{
                    $data[$k][$i]["key"] = $k1;
                    $data[$k][$i]["value"] = $v1;
                }

                $i++;
            }

        }

        return $data;
    }

    /**
     * 根据typeid返回对应type内容
     * @return obj base数据
     */
    public function getMetaDataByType() {
        $domain = new DomainMetaData();
        $rs = $domain->getMetaDataByType($this->code_type);
        return $rs;
    }


}
